// myCenter.js
import {
    imgUrl,
    baseUrl
} from '../../network/requestUrl'
import {
    parsingCodeToken
} from '/../../network/api'
// 获取应用实例
const app = getApp()
const userAvatar = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
    data: {
        avatarUrl: wx.getStorageSync('avatarUrl') ? `${baseUrl}${wx.getStorageSync('avatarUrl')}` : userAvatar,
        nickName: '',
        pageTitle: '我的',
        statusBarHeight: 0,
        navBarHeight: 0,
        label1: ['外出申请', '我的套餐', '产检计划', '健康报告'],
        label2: ['会员福利', '二胎专享礼', '关注服务号'],
        imgs1: ['/images/waichu.png', '/images/wodetaocan.png', '/images/chanjianjihua.png', '/images/tijianbaogao.png'],
        imgs2: ['/images/fuli.png', '/images/ertai.png', '/images/guanzhu.png'],
        isShowBirth: false,
        currentDate: new Date().getTime(),
        minDate: new Date().getTime(),
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            if (type === 'day') {
                return `${value}日`;
            }
            return value;
        },
        imgUrl: `${imgUrl}/beijingwd.png`
    },

    onInput(event) {
        this.setData({
            currentDate: event.detail,
        });
    },

    // 我的孕期
    onChildbirth() {
        this.setData({
            isShowBirth: !this.data.isShowBirth
        })
    },

    // 在线客服
    goOnOrder() {
        wx.navigateTo({
            url: '/pages/onOrder/index',
        })
    },

    // 厨师端
    onChef() {
        wx.navigateTo({
            url: '/pages/chef/index',
        })
    },

    // 产康师
    onChankang() {
        wx.navigateTo({
            url: '/pages/chankang/index',
        })
    },

    // 护理中心
    onHlCenter() {
        wx.navigateTo({
            url: '/pages/hlCenter/index',
        })
    },

    // 我的应用
    onRouter1(e) {
        const {
            name
        } = e.currentTarget.dataset
        let path;
        if (name === '外出申请') {
            path = '/pages/applicationOut/index'
        }
        if (name === '我的套餐') {
            path = '/pages/myPackage/index'
        }
        if (name === '产检计划') {
            path = '/pages/cjPlan/index'
        }
        if (name === '健康报告') {
            path = '/pages/healthReport/index'
        }
        if (path) {
            wx.navigateTo({
                url: path,
            })
        }
    },

    // 我的福利页面跳转
    onRouter3(e) {
        const {
            name
        } = e.currentTarget.dataset
        let path;
        if (name === '关注服务号') {
            path = '/pages/followService/index'
        }
        if (name === '二胎专享礼') {
            path = '/pages/secondChildGift/index'
        }
        if (name === '会员福利') {
            path = '/pages/memberBenefits/index'
        }
        if (path) {
            wx.navigateTo({
                url: path,
            })
        }
    },

    getNavBarSize() {
        //获取状态栏高度
        const statusBarHeight = wx.getSystemInfoSync().statusBarHeight
        //获取小程序胶囊信息
        const menu = wx.getMenuButtonBoundingClientRect()
        //计算获得导航栏高度（此处为重点!!!）
        const navBarHeight = (menu.top - statusBarHeight) * 2 + menu.height
        this.setData({
            statusBarHeight: statusBarHeight,
            navBarHeight: navBarHeight
        })
    },
    // 修改头像
    changeUserAvatar() {
        wx.navigateTo({
            url: '/pages/addUserInfo/index',
        })
    },
    getUserInfo() {
        if (!wx.getStorageSync('token')) {
            this.onGetLogin()
        }
        console.log('have token')
        let that = this
        const ava = wx.getStorageSync('avatarUrl')
        const nick = wx.getStorageSync('nickName')
        if (ava && nick) {
            that.setData({
                nickName: nick, //将得到的缓存给key 
                avatarUrl: `${baseUrl}${ava}`
            })
        } else {
            that.setData({
                avatarUrl: userAvatar,
                nickName: '普通用户'
            })
        }
    },
    async getToken(code) {
        const res = await parsingCodeToken({
            code: code
        })
        if (res && res.code === 200) {
            wx.setStorageSync('token', res.data.token)
            wx.setStorageSync('sessionKey', res.data.sessionkey)
            this.setData({
                nickName: res.data.nickName,
                avatarUrl: (res.data.avatar && res.data.avatar !== '') ? `${baseUrl}${res.data.avatar}` : userAvatar
            }, () => {
                wx.setStorageSync('nickName', this.data.nickName)
                wx.setStorageSync('avatarUrl', this.data.avatarUrl)
            })
        }
    },
    // 自动登陆
    onGetLogin() {
        const that = this
        wx.login({
            success(res) {
                if (res.code) {
                    // 调用接口获取token及用户基本信息
                    that.getToken(res.code)
                }
            }
        })
    },
    onLoad() {
        this.getUserInfo()
        this.getNavBarSize()
    },
})